package com.spa.common.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import java.util.Locale;

import io.netty.util.internal.StringUtil;

/**
 * @version v1.0
 * @ProjectName: remote-mgmt-service
 * @ClassName: DateUtil
 * @Author: P0152257
 * @Date: 1/5/2021 11:50 AM
 */
public class DateUtil {

	/**
	 * 默认日期格式
	 */
	public static String DEFAULT_FORMAT = "yyyy-MM-dd";

	/**
	 * 获取下年的最后一天
	 * 
	 * @param year
	 * @return
	 */
	public static Date getNextYearLast() {
		Calendar currCal = Calendar.getInstance(Locale.CHINA);
		int currentYear = currCal.get(Calendar.YEAR) + 1;
		return getYearLast(currentYear);
	}

	public static boolean checkDates(Date date, String start, String end) {
		String specificDate = fmDate(date, DEFAULT_FORMAT);
		Date startDate = getStringToDate(specificDate + " " + start);
		Date endDate = getStringToDate(specificDate + " " + end);
		if (date.getTime() > startDate.getTime() && endDate.getTime() > date.getTime()) {
			return true;
		} else {
			return false;
		}

	}

	/**
	 * 获取永久2099-12-31
	 *
	 * @return
	 */
	public static Date getForever() {
		return getYearLast(2099);
	}

	/**
	 * 获取下年的第一天
	 *
	 * @return
	 */
	public static Date getNextYearFirst() {
		Calendar currCal = Calendar.getInstance(Locale.CHINA);
		int currentYear = currCal.get(Calendar.YEAR) + 1;
		return getYearFirst(currentYear);
	}

	/**
	 * 根式时间转换
	 * 
	 * @param year
	 * @return
	 */
	public static Date getStringToDate(String formatTime) {
		DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date myDate2 = null;
		try {
			myDate2 = dateFormat2.parse(formatTime);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return myDate2;
	}

	/**
	 * 获取某年最后一天日期
	 * 
	 * @param year 年份
	 * @return Date
	 */
	public static Date getYearLast(int year) {
		Calendar calendar = Calendar.getInstance(Locale.CHINA);
		calendar.clear();
		calendar.set(Calendar.YEAR, year);
		calendar.roll(Calendar.DAY_OF_YEAR, -1);
		calendar.set(Calendar.HOUR_OF_DAY, 23 - 8);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		Date currYearLast = calendar.getTime();

		return currYearLast;
	}

	/**
	 * 获取某年第一天日期
	 *
	 * @param year 年份
	 * @return Date
	 */
	public static Date getYearFirst(int year) {
		Calendar calendar = Calendar.getInstance(Locale.CHINA);
		calendar.clear();
		// 设置年份
		calendar.set(Calendar.YEAR, year);
		// 设置月份
		calendar.set(Calendar.MONTH, 0);
		// 获取某月最小天数
		int firstDay = calendar.getMinimum(Calendar.DATE);
		// 设置日历中月份的最小天数
		calendar.set(Calendar.DAY_OF_MONTH, firstDay);
		Date currYearFirst = calendar.getTime();
		return currYearFirst;
	}
//	public static void main(String[] args) {
//		Date currYearLast = getYearLast(2023);
//		System.out.println(currYearLast);
//	}

	private DateUtil() {
		throw new IllegalStateException("Utility class");
	}

	public static void main(String[] args) {
		System.out.println(getDayAgoOrAfterString(3));
	}

	/**
	 *
	 * @param num ﹣3 三天前 +3 三天后
	 * @return
	 */
	public static Date getDayAgoOrAfterString(int num) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, num);
		return calendar.getTime();
	}

	/**
	 * 获取当前时间
	 * 
	 * @param
	 * @return java.util.Date
	 * @author changguiying
	 * @date 1/5/2021 11:50 AM
	 */
	public static Date getNowDate() {
		return new Date();
	}

	public static String fmDate(Date createTime) {
		SimpleDateFormat fm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		fm.setTimeZone(TimeZone.getTimeZone("GMT+8"));
		return fm.format(createTime);
	}

	public static String fmDate(Date createTime, String format) {
		SimpleDateFormat fm = new SimpleDateFormat(format);
		fm.setTimeZone(TimeZone.getTimeZone("GMT+8"));
		return fm.format(createTime);
	}

	public static boolean isAvailable(String last) {
		if (StringUtil.isNullOrEmpty(last)) {
			return false;
		}
		String dateFormat = "yyyy-MM-dd HH:mm:ss";
		long date = 0;
		if (isDateString(last, dateFormat)) {
			Date dateTime = cn.hutool.core.date.DateUtil.parse(last, dateFormat);
			date = dateTime.getTime();
		} else {
			date = Long.parseLong(last);
		}
		return System.currentTimeMillis() / 1000 - 30 * 60 <= date;
	}

	private static boolean isDateString(String datevalue, String dateFormat) {
		try {
			SimpleDateFormat fmt = new SimpleDateFormat(dateFormat);
			java.util.Date dd = fmt.parse(datevalue);
			return datevalue.equals(fmt.format(dd));
		} catch (Exception e) {
			return false;
		}
	}

	/**
	 * 是否到期
	 * 
	 * @param dueDate 到期日期
	 * @return boolean true到期； false未到期
	 */
	public static boolean isDue(Date dueDate) {
		return dueDate.getTime() < System.currentTimeMillis();
	}

}
